package com.example.back.mapper;

import com.example.back.dto.req.ZDrugListReq;
import com.example.back.dto.resp.ZDrugInfoRsp;
import com.example.back.dto.resp.ZDrugListRsp;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.util.List;

@Component
public interface ZDrugMapper {

    @SelectProvider(type = ZDrugMapper.CommonProvider.class,method = "List")
    List<ZDrugListRsp> List(@Param("zDrugListReq") ZDrugListReq zDrugListReq);

    @Select("SELECT a.drug_name_nvarchar AS DrugName,a.py_nvarchar AS PY,a.price_money AS Price,a.img_nvarchar AS Img " +
            " FROM z_drug_base_info a WHERE a.drug_id_bigint=#{did}")
    ZDrugInfoRsp Info(@Param("did") Long did);

    class CommonProvider {
        public String List(ZDrugListReq zDrugListReq){
            SQL sql = new SQL();
            sql.SELECT("a.drug_id_bigint AS DrugID,a.drug_name_nvarchar AS DrugName,a.py_nvarchar AS PY,a.disable_bit AS Disable,a.price_money AS Price " +
                    "FROM z_drug_base_info  a");

            if (!ObjectUtils.isEmpty(zDrugListReq.getName())){
                String name = zDrugListReq.getName();
                sql.WHERE("a.drug_name_nvarchar LIKE CONCAT ('%',#{name},'%')");
            }
            if (!ObjectUtils.isEmpty(zDrugListReq.getDisable())){
                int disable = 1;
                if ("false".equals(zDrugListReq.getDisable())){
                    disable=0;
                }
                sql.WHERE("a.disable_bit =${disable}");
            }
            sql.ORDER_BY("a.drug_name_nvarchar DESC");
            return sql.toString();
        }
    }
}
